from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from driver_utils import DriverUtils


class BasePage:

    # 都可以调用打开最大化浏览器
    def __init__(self):
        self.driver = DriverUtils.open_deriver()

    # 输入url
    def get_url(self, url):
        self.driver.get(url)

    # 显示等待定位元素
    def get_element(self, by, element):
        return WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_element(by, element))

    # 显示等待定位列表
    def get_elements(self, by, element):
        return WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_elements(by, element))

    # 点击元素
    def element_click(self, by, element):
        self.get_element(by, element).click()

    # 输入元素
    def element_send_keys(self, by, element, keys):
        self.get_element(by, element).send_keys(keys)

    # 获取元素文本
    def element_text(self, by, element):
        return self.get_element(by, element).text

    # 获取列表中所有的元素文本
    def elements_text(self, by, element):
        listText = []
        text = self.get_elements(by, element)
        for x in text:
            listText.append(x.text)
        return listText

    # 切换HTML中的iframe框架
    def element_iframe(self, by, element):
        self.driver.switch_to.frame(self.get_element(by, element))

    # 恢复默认的HTML框架
    def element_default(self):
        self.driver.switch_to.default_content()

    # 点击元素下拉列表:index下标
    def element_down_index(self, by, element, index):
        Select(self.get_element(by, element)).select_by_index(index)

    # 鼠标悬停
    def element_move_to(self, by, element):
        ActionChains(self.driver).move_to_element(self.get_element(by, element)).perform()

    # 鼠标右键
    def element_context_click(self, by, element):
        ActionChains(self.driver).context_click(self.get_element(by, element)).perform()

    # 鼠标双击
    def element_double_click(self, by, element):
        ActionChains(self.driver).double_click(self.get_element(by, element)).perform()

    # js弹窗中获取文本
    def element_alert_text(self):
        self.driver.switch_to.alert.text()

    # js弹窗点击确定按钮
    def element_alert_yes(self):
        self.driver.switch_to.alert.accept()

    # js弹窗点击取消按钮
    def element_alert_no(self):
        self.driver.switch_to.alert.dismiss()

    # 切换网页窗口:index为下标
    def element_window_handles(self, index):
        self.driver.switch_to.window(self.driver.window_handles[index])

    # 网页中的滚动条(x为横坐标,y为纵坐标)
    def element_window_scro(self, x, y):
        self.driver.execute_script(f"window.scrollTo({x}, {y})")

    # 元素中的滚动条(x为横坐标,y为纵坐标)
    def element_scro(self, by, element, x, y):
        self.driver.execute_script(f"document.{self.get_element(by, element)}.scrollTo({x},{y});")

    # 后退网页
    def element_back_window(self):
        self.driver.back()

    # 前进网页
    def element_froward_window(self):
        self.driver.forward()

    # 刷新网页
    def element_refresh_window(self):
        self.driver.refresh()

    # 截图 图片文件路径.png
    def element_png(self, file):
        self.driver.get_screenshot_as_file(file)

    # 元素是否可见
    def element_is_displayed(self, by, element):
        self.get_element(by, element).is_displayed()

    # 元素是否可用
    def element_is_enabled(self, by, element):
        self.get_element(by, element).is_enabled()

    # 元素是否已被选中
    def element_is_selected(self, by, element):
        self.get_element(by, element).is_selected()


